from uliweb.orm import *
from uliweb.contrib.auth.models import User

class UserProfile(Model):
    email = Field(str, max_length=40)
    image = Field(str, max_length=128)
    user = OneToOne(User)
    
    def get_image_url(self):
        from uliweb.contrib.upload import get_url
        from uliweb.core.SimpleFrame import local
        from uliweb.contrib.staticfiles import url_for_static
        
        if self.image:
            return get_url(self.image, subfolder='portrait', application=local.application)
        else:
            return url_for_static('images/user.jpg')
    
def get_profile(user):
    p = user.userprofile
    if not p:
        p = UserProfile(user=user)
        p.save()
    return p